﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class Physician
    {
        partial void FullName_Compute(ref string result)
        {
            // Set result to the desired field value
            result = this.LastName + ", " + this.FirstName;
        }

        partial void FirstName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.FirstName != null)
                this.FirstName = (this.FirstName.Substring(0, 1).ToUpper() + this.FirstName.Substring(1).ToLower()).Trim();
        }

        partial void Phone_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.FirstName != null && this.LastName != null && this.Phone != null)
            {
                if(this.DataWorkspace.ApplicationData.Physicians.Where(physicians =>
                    physicians.FirstName == this.FirstName && physicians.LastName == this.LastName && 
                    physicians.Phone == this.Phone && physicians.Id != this.Id).FirstOrDefault() != null)
                    results.AddPropertyError("Duplicated physicians: " + this.FirstName + " (" + this.Phone + ")");
            }
        }

        partial void LastName_Validate(EntityValidationResultsBuilder results)
        {
            // results.AddPropertyError("<Error-Message>");
            if (this.LastName != null)
                this.LastName = (this.LastName.Substring(0, 1).ToUpper() + this.LastName.Substring(1).ToLower()).Trim();
        }
    }
}
